﻿using System;
using System.Threading.Tasks;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ShineTech.Domain.SeedWork;
using ShineTech.Web.Helpers;
using ShineTech.Web.Missons.Commands;
using ShineTech.Web.Missons.Requests;

namespace ShineTech.Web.Missons
{
    [Route("api/[controller]")]
    [ApiController]
    public class MissonController : BaseController
    {
        private readonly IMediator _mediator;

        public MissonController(IMediator mediator)
        {
            _mediator = mediator;

        }

        [HttpGet("{id}")]
        [ProducesResponseType(typeof(MissonDTO), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<IActionResult> Get([FromRoute]Guid id)
        {
            MissonDTO misson = await _mediator.Send(new MissonByIdQuery(id));
            if (misson == null)
                return NotFound();
            return Ok(misson);
        }



        [HttpGet(Name = "MissonPageByFiltersPage")]
        [ProducesResponseType(typeof(PageData<MissonDTO>), StatusCodes.Status200OK)]
        public async Task<IActionResult> GetPage([FromQuery] string name,Guid kanbanId, int pageIndex, int pageSize)
        {
            var user = GetCurrentUser();
            PageData<MissonDTO> result = await _mediator.Send(new MissonsByFiltersPageQuery(name, user.Id, kanbanId, pageIndex, pageSize));
            return Ok(result);
        }

        /// <summary>
        /// 创建 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(MissonDTO), StatusCodes.Status201Created)]
        [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
        public async Task<IActionResult> Post([FromBody] CreateMissonRequest request)
        {
            try
            {
                if (!ModelState.IsValid)
                    return BadRequest(ModelState);
                var currentUser = GetCurrentUser();
                var command = new CreateMissonCommand(request.KanbanId, request.Name, DateTime.Now,  currentUser.Id, currentUser.Name);
                MissonDTO misson = await _mediator.Send(command);
                return Created(string.Empty, misson);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [ProducesResponseType(typeof(MissonDTO), StatusCodes.Status201Created)]
        [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
        public async Task<IActionResult> Put([FromBody] UpdateMissonRequest request)
        {
            try
            {
                if (!ModelState.IsValid)
                    return BadRequest(ModelState);
                var currentUser = GetCurrentUser();
                var command = new UpdateMissonCommand(request.Id, request.Name, currentUser.Id, currentUser.Name, DateTime.Now);
               MissonDTO misson = await _mediator.Send(command);
                return Created(string.Empty, misson);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("Complete")]
        [ProducesResponseType(typeof(MissonDTO), StatusCodes.Status201Created)]
        [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
        public async Task<IActionResult> Complete([FromBody] CompleteMissonRequest request)
        {
            try
            {
                if (!ModelState.IsValid)
                    return BadRequest(ModelState);
                var currentUser = GetCurrentUser();
                var command = new CompletedMissonCommand(request.Id, true, currentUser.Id, currentUser.Name, DateTime.Now);
                MissonDTO misson = await _mediator.Send(command);
                return Created(string.Empty, misson);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }

        }

        
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("Delete")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<IActionResult> Delete([FromQuery]Guid id)
        {
            try
            {
                var user = GetCurrentUser();
                await _mediator.Publish(new DeleteMissonCommand(DateTime.Now, id, user.Id, user.Name));
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

    }
}
